package edu.gatech.cs4400.spring2014.A8.model;
import java.sql.*;

/**
 * Singleton class that holds the information to connect to the database.
 * Initialized with the "connect" method. This should be called in the
 * initial setup of the application.
 * 
 * @author Stephen
 *
 */
public abstract class ConnectionManager {
	//Information to connect to the database
	private static final String DBUSERNAME = "cs4400_Group_8";
	private static final String DBPASSWORD = "TpEvbGyP";
	private static final String DBHOST = "academic-mysql.cc.gatech.edu";
	
	public static Connection connection = null;
	public static Statement statement = null;
	private static boolean initialized = false;
	
	/**
	 * Form a connection to the database. Store this in the global variables
	 *  "connection" and "statement"
	 * @return success of connection
	 */
	public static boolean connect() {
		if (!initialized) {
			//code adapted from: http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html
			String hosturl = String.format("jdbc:mysql://%s/%s", DBHOST, DBUSERNAME);
			try {
				connection = DriverManager.getConnection(hosturl, DBUSERNAME, DBPASSWORD);
				statement = connection.createStatement();
				initialized = true;
			}
			catch(SQLException e) {
				System.out.printf("Unable to establish connection. [%s]\n", hosturl);
				System.out.println(e.getMessage());
				System.out.println("Did you forget to connect to VPN?");
			}
		}
		return initialized;
	}
}
